package com.unknown.wificontrol.ui;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.unknown.wificontrol.R;
import com.unknown.wificontrol.listener.IUIBaseMethod;
import com.unknown.wificontrol.listener.OnDialogListener;
import com.unknown.wificontrol.util.Constant;
import com.unknown.wificontrol.util.Preference;
import com.unknown.wificontrol.widget.CustomDialog;

import java.util.Locale;

import butterknife.ButterKnife;

public abstract class BaseActivity extends AppCompatActivity implements IUIBaseMethod {

    protected final String BUNDLE = "bundle";
    protected Activity mActivity;

    private CustomDialog progressDialog;
    private TextView tvprogressMsg;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity = this;
        setContentView(getLayout());
        ButterKnife.bind(this);
        initDialog();
        initViews();
    }

    private void initDialog() {
        progressDialog = new CustomDialog(this, R.layout.layout_loading_dialog);
        progressDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        tvprogressMsg = findView(progressDialog, R.id.tv_message);
    }

    public void onBack(View v) {
        finish();
    }

    /**
     * 界面跳转
     *
     * @param targetClass ：跳转的目标界面
     * @param bundle      :传入的参数
     */
    protected void changeView(Class targetClass, Bundle bundle) {
        Intent intent = new Intent(this, targetClass);
        if (null != bundle) {
            intent.putExtra(BUNDLE, bundle);
        }
        startActivity(intent);

    }

    /**
     * 界面跳转
     *
     * @param targetClass ：跳转的目标界面
     */
    protected void changeView(Class targetClass) {
        Intent intent = new Intent(this, targetClass);
        startActivity(intent);
    }

    /**
     * 界面跳转
     *
     * @param targetClass 跳转class
     * @param bundle      数据参数
     * @param requestCode
     */
    protected void changeViewForResult(Class targetClass, Bundle bundle, int requestCode) {
        Intent intent = new Intent(this, targetClass);
        if (null != bundle) {
            intent.putExtra(BUNDLE, bundle);
        }
        startActivityForResult(intent, requestCode);

    }

    /**
     * 界面跳转
     *
     * @param targetClass ：跳转的目标界面
     * @param bundle      :传入的参数
     * @param isFinish    ：是否关闭当前的activity
     */
    protected void changeView(Class targetClass, Bundle bundle, boolean isFinish) {
        Intent intent = new Intent(this, targetClass);
        if (null != bundle) {
            intent.putExtra(BUNDLE, bundle);
        }
        startActivity(intent);
        if (isFinish) {
            finish();
        }
    }

    protected <T extends View> T findView(int id) {
        return (T) findViewById(id);
    }

    protected <T extends View> T findView(View v, int id) {
        return (T) v.findViewById(id);
    }


    public <T extends View> T findView(Dialog dlg, int id) {
        return (T) dlg.findViewById(id);
    }


    public void showToast(String text) {
        Toast result = new Toast(this);
        LayoutInflater inflate = LayoutInflater.from(this);
        View v = inflate.inflate(R.layout.layout_toast, null);
        TextView tv = v.findViewById(R.id.tv_message);
        tv.setText(text);
        result.setView(v);
        result.setGravity(Gravity.CENTER, 0, 0);
        result.setDuration(Toast.LENGTH_SHORT);
        result.show();
    }

    protected boolean isEmpty(String str) {
        return TextUtils.isEmpty(str) || str.toLowerCase(Locale.getDefault()).trim().equals("null");
    }

    protected void showProgress(String text) {
        if (isFinishing())
            return;
        if (progressDialog != null) {
            tvprogressMsg.setText(text);
        }
        if (!progressDialog.isShowing()) {
            progressDialog.show();
        }
    }

    protected void showProgress() {
        if (isFinishing())
            return;
        if (!progressDialog.isShowing()) {
            progressDialog.show();
        }
    }

    protected void hideProgress() {
        if (isFinishing())
            return;
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
    }

    protected String getHost() {
        String host = Preference.get(this, Constant.TAG_IP, Constant.defaultIP);
        return host.startsWith("http://") ? host : String.format("http://%s", host);
    }

    protected void showSuccess(String message, final OnDialogListener listener) {
        showDialog("温馨提示", message, "", "确定", listener);
    }

    protected void showTip(String message, final OnDialogListener listener) {
        showDialog("温馨提示", message, "取消", "确定", listener);
    }

    protected void showDialog(String title, String message, String btnRight, final OnDialogListener listener) {
        showDialog(title, message, "取消", btnRight, listener);
    }

    protected void showDialog(String title, String message, String showLeft, String btnRight, final OnDialogListener listener) {
        final CustomDialog diyDialog = new CustomDialog(this, R.layout.layout_dialog);
        TextView tv_title = diyDialog.findViewById(R.id.tv_title);
        tv_title.setText(title);
        TextView tv_message = diyDialog.findViewById(R.id.tv_message);
        tv_message.setText(message);

        Button btn_ok = diyDialog.findViewById(R.id.btn_ok);
        Button btn_cancel = diyDialog.findViewById(R.id.btn_cancel);
        if (isEmpty(showLeft)) {
            btn_cancel.setVisibility(View.GONE);
        } else {
            btn_cancel.setVisibility(View.VISIBLE);
        }
        btn_cancel.setVisibility(!isEmpty(showLeft) ? View.VISIBLE : View.GONE);
        btn_cancel.setText(showLeft);

        btn_ok.setText(btnRight);
        diyDialog.setCancelable(false);
        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                diyDialog.dismiss();
                if (listener != null) {
                    listener.onLeft();
                }
            }
        });
        btn_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                diyDialog.dismiss();
                if (listener != null) {
                    listener.onConfirm();
                }
            }
        });

        Window window = diyDialog.getWindow();
        window.setGravity(Gravity.CENTER);
        window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
        diyDialog.show();
    }

    protected View emptyData(String text) {
        View view = LayoutInflater.from(this).inflate(R.layout.layout_empty, null);
        TextView tv_value = view.findViewById(R.id.tvValue);
        tv_value.setText(text);
        return view;
    }

}
